package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
/**
 *
 * @author axi
 *
 */
public class LanzhouNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://news.lanzhou.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]{9}.shtml",
				"http://lz.lanzhou.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]{9}.shtml"};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta meta=null;
		String html=urlMeta.getHtml();
		String encoding=urlMeta.getEncoding();
		String url=urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
		}
		String htmltxt=urlMeta.getHtml();
		String title="";
		String content="";
		Long date=null;
		Integer commentNum=0;
		Integer clickNum=0;
		NewsMeta news=new NewsMeta();
		Document doc=Jsoup.parse(htmltxt);
		/**
		 * 解析 标题 <h2>XXXXXXXXXX</h2> 定位
		 */
		title=doc.select("h2").text();
		/**
		 * 解析时间
		 */
		Elements selectDate=doc.select("p[style=font-size:12px; display:block;]");
		if(selectDate.size() > 0){
			Pattern DATE=Pattern.compile("[0-9]{1,2}-[0-9]{1,2}-[0-9]{1,2}");
			Pattern TIME=Pattern.compile("[0-9]{1,2}[:][0-9]{1,2}");
			Matcher matcher_date=DATE.matcher(selectDate.toString());
			String _date="";
			if (matcher_date.find()) {
				 String group=matcher_date.group();
				 _date="20" + group;
			}
			Matcher matcher_time=TIME.matcher(selectDate.toString());
			if (matcher_time.find()) {
				 String group=matcher_time.group();
				 _date += " "+group;
			}
			if(!"".equals(_date)){
				date=DateUtils.FormString(_date);
			}
		}
		/**
		 * 解析内容
		 */
		Elements select=doc.select("div[style=width:690px; height:auto;background-color:#FCFCFC;]");
		if(select.size() > 0){
			Elements elements=select.select("div");
			if(elements.size() > 0){
				content=HtmlCleaner.getContentHtml(url,elements.get(3));
			}
		}

		String author=JSoupUtils.matchAuthor(doc, "稿源：");
		news.setAuthor(author);
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		news.setUrl(url);
		news.setType(0);
		news.setCommentNum(commentNum);
		news.setClickNum(clickNum);
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LanzhouNewsAnalyse l=new LanzhouNewsAnalyse();
		String url="http://lz.lanzhou.cn/system/2012/11/22/010282838.shtml";
//		String url="http://news.lanzhou.cn/system/2012/11/22/010282865.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=l.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
